﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ZhongKeSite.Common;
using ZhongKeSite.Common.Helper;
using ZhongKeSite.Entity;
using ZhongKeSite.Service;
using ZhongKeSite.Web.Models;

namespace ZhongKeSite.Web.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class DownLoadController : BaseController
    {
        IDownLoadService downLoadService;
        IBasisService basisService;
        public DownLoadController(IDownLoadService _downLoadService, IBasisService _basisService)
        {
            downLoadService = _downLoadService;
            basisService = _basisService;
        }

        #region DownLoad模块
        [RoleAuthorize("DownLoadList")]
        public IActionResult DownLoadList(string search = "",string categoryID = "", int pageindex = 1, int pagesize = 15)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    Expression<Func<DownLoad, bool>> predicate = s => s.IsDel == 0;
                    if (!string.IsNullOrWhiteSpace(search))
                    {
                        predicate = predicate.And(k => k.Title.Contains(search));
                    }
                    if (!string.IsNullOrWhiteSpace(categoryID))
                    {
                        predicate = predicate.And(k => k.CategoryID == categoryID);
                    }
                    return Json(downLoadService.GetDownLoadPagedList(pageindex, pagesize, predicate));
                }
                ViewBag.DownLoadTypeDLL = basisService.GetCategoryListDLL(CategoryType.DownLoadType.ToString(),false,true);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("DownLoadAdd")]
        public IActionResult DownLoadAdd()
        {
            try
            {
                DownLoad model = new DownLoad();
                ViewBag.DownLoadTypeDLL = basisService.GetCategoryListDLL(CategoryType.DownLoadType.ToString());
                return View(model);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("DownLoadAdd")]
        public IActionResult DownLoadAdd(DownLoad model)
        {
            try
            {
                model.AddBy = CurrentUser.Login;
                return Json(downLoadService.AddDownLoad(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("DownLoadDel")]
        public IActionResult DownLoadDel(string id)
        {
            try
            {
                List<string> ids = new List<string>() { id };
                return Json(downLoadService.UpdateDownLoadState(ids, CurrentUser.Login, DataStatus.Deleted));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("DownLoadEdit")]
        public IActionResult DownLoadEdit(string id)
        {
            try
            {
                ResultResponse<DownLoad> result = downLoadService.GetDownLoad(id);
                ViewBag.DownLoadTypeDLL = basisService.GetCategoryListDLL(CategoryType.DownLoadType.ToString());
                return View(result.data);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("DownLoadEdit")]
        public IActionResult DownLoadEdit(DownLoad model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(downLoadService.UpdateDownLoad(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion

    }
}